//
//  UIToolBarView.m
//  Assistant
//
//  Created by Hunk on 12-3-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIToolBarView.h"

#define WIDTH_BARBUTTON        (65.0f)
#define TAG_SELECTALLBUTTON    (12)
#define TAG_SHORTMESSAGEBUTTON (13)
#define TAG_EMAILBUTTON        (14)
#define TAG_EDITBUTTON         (15)
#define TAG_TOOLBAR            (16)

@implementation UIToolBarView
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // FlexibleSpace
        UIBarButtonItem* flexibleSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // Cache items
         _barButtonItemArray      = [[NSMutableArray alloc] init];
        NSMutableArray *itemArray = [[NSMutableArray alloc] init];
        NSArray *titleArray       = [NSArray arrayWithObjects:
                                     NSLocalizedString(@"全选", nil), 
                                     NSLocalizedString(@"短信", nil), 
                                     NSLocalizedString(@"邮件", nil), 
                                     NSLocalizedString(@"编辑", nil), nil];
       
        UIBarButtonItem *barButtonItem[4];
        for(int i = 0; i < 4; i++)
        {
            barButtonItem[i] = [[[UIBarButtonItem alloc] initWithTitle:[titleArray objectAtIndex:i]
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(toolBarButtonHandle:)] autorelease];
            [barButtonItem[i] setTag:i];
            [barButtonItem[i] setWidth:WIDTH_BARBUTTON];
            [itemArray addObject:barButtonItem[i]];
        }
        
        [_barButtonItemArray addObjectsFromArray:itemArray];
        
        [itemArray insertObject:flexibleSpaceBarButtonItem atIndex:0];
        [itemArray addObject:flexibleSpaceBarButtonItem];
        [flexibleSpaceBarButtonItem release];
        
        UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolBar.tintColor = TINT_COLOR;
        [toolBar setBarStyle:UIBarStyleDefault];
        [toolBar setItems:itemArray animated:YES];
        [itemArray release];
        [self addSubview:toolBar];
        [toolBar release];
    }
    return self;
}

- (void)setDelegate:(id<UIToolBarViewDelegate>)delegate
{
    if(_delegate != delegate)
    {
        [_delegate release];
        
        _delegate = [delegate retain];
    }
}

- (UIBarButtonItem *)barButtonItemAtIndex:(NSInteger)index
{
    return [_barButtonItemArray objectAtIndex:index];
}

- (void)toolBarButtonHandle:(id)sender
{
    UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
    
    if(_delegate && [_delegate respondsToSelector:@selector(toolBarView:didSelectBarButtonAtIndex:)])
    {   
        [_delegate toolBarView:self didSelectBarButtonAtIndex:[barButton tag]];
    }
}

- (void)dealloc
{
    SAFE_RELEASE(_barButtonItemArray)
    SAFE_RELEASE(_delegate)
    
    [super dealloc];
}

@end
